﻿using System;
using System.Net.Mail;
using Altairis.MailToolkit;

namespace SampleListManager {
    public partial class Verify : System.Web.UI.Page {

        protected void Page_Load(object sender, EventArgs e) {
            var operation = this.Request.QueryString["op"].ToUpper();
            var email = new MailAddress(this.Request.QueryString["e"]);
            var code = this.Request.QueryString["c"];
            var listName = this.Request.QueryString["l"];

            if (operation.Equals("SUB")) {
                // Subscribe
                var result = MailingListManager.SubscribeConfirm(listName, email, code);
                if (result) {
                    this.LiteralMessage.Text = string.Format("Address <code>{0}</code> was subscribed to {1}.",
                        email.Address,                                      // 0
                        MailingListManager.Lists[listName].Description);    // 1
                }
                else {
                    this.LiteralMessage.Text = string.Format("Subscription to {0} failed - invalid confirmation code or you are already subscribed.",
                        MailingListManager.Lists[listName].Description);    // 0
                }
            }
            else {
                // Remove
                var result = MailingListManager.RemoveConfirm(listName, email, code);
                if (result) {
                    this.LiteralMessage.Text = string.Format("Address <code>{0}</code> was removed from {1}.",
                        email.Address,                                      // 0
                        MailingListManager.Lists[listName].Description);    // 1
                }
                else {
                    this.LiteralMessage.Text = string.Format("Removal from {0} failed - invalid confirmation code or you are already removed.",
                        MailingListManager.Lists[listName].Description);    // 0
                }
            }
        }

    }
}